package com.itpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itpeng.constant.MessageConstant;
import com.itpeng.entity.PageResult;
import com.itpeng.entity.QueryPageBean;
import com.itpeng.entity.Result;
import com.itpeng.pojo.CheckGroup;
import com.itpeng.service.CheckGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/20  14:17
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }
    /**
     * 根据条件分页查询
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult<CheckGroup> checkGroupPageResult =  checkGroupService.findPage(queryPageBean);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupPageResult);

    }

    /**
     * 添加数据
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){

        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据ID查询检查项
     */
    @GetMapping("findById")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
     * 修改数据
     * @param checkGroup
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        checkGroupService.update(checkGroup, checkitemIds);

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    public Result deleteById(Integer id){

        checkGroupService.deleteById(id);

        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 获取选中的检查项id
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
    }

}
